﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuanLyDeTaiKHCN.Obj_class;
namespace QuanLyDeTaiKHCN.UserControls.Others
{
    public partial class ThongTinPhatSinh : System.Web.UI.UserControl
    {
        _dbDataContext _db = new _dbDataContext();
        SecurityLib.Sec_AES _ps = new SecurityLib.Sec_AES();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
            if (inputID.Value == "")//truong hop them moi
            {
                string str = Request.QueryString["id"].Replace(" ", "+");
                string madetai = _ps.Decrypt(str, "gold", 128);
                tblGhiChu _tb = new tblGhiChu();
                _tb.MaDeTai = madetai;
                if (CatalogAccess.ConvertToDateBool(txtNgayPhatSinhThongTin.Text))
                    _tb.NgayGhiChu = CatalogAccess.ConvertToDate(txtNgayPhatSinhThongTin.Text);
                _tb.NoiDung = txtNoiDung.Text;
                _tb.Lan = CatalogAccess.GetLanGhiChu(madetai);
                try
                {
                    _db.tblGhiChus.InsertOnSubmit(_tb);
                    _db.SubmitChanges();
                    lblstatus.Text = "Đã lưu.";
                    BindGrid();
                }
                catch { lblstatus.Text = "Không lưu được."; }
            }
            else//truong hop cap nhat
            {
                var _tb = _db.tblGhiChus.Single(r => r.ID == int.Parse(inputID.Value));
                if (CatalogAccess.ConvertToDateBool(txtNgayPhatSinhThongTin.Text))
                    _tb.NgayGhiChu = CatalogAccess.ConvertToDate(txtNgayPhatSinhThongTin.Text);
                _tb.NoiDung = txtNoiDung.Text;
                try
                {
                    _db.SubmitChanges();
                    lblstatus.Text = "Đã lưu.";
                    BindGrid();
                }
                catch { lblstatus.Text = "Không lưu được."; }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            txtNgayPhatSinhThongTin.Text = "";
            txtNoiDung.Text = "";
            inputID.Value = "";
        }

        protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var item = _db.tblGhiChus.Single(r => r.ID == int.Parse(grid.DataKeys[e.RowIndex].Value.ToString()));
            try
            {
                _db.tblGhiChus.DeleteOnSubmit(item);
                _db.SubmitChanges();
                BindGrid();
                inputID.Value = "";
            }
            catch { }
        }

        protected void grid_RowEditing(object sender, GridViewEditEventArgs e)
        {
            inputID.Value = ((Label)grid.Rows[e.NewEditIndex].FindControl("lblID")).Text;
            txtNgayPhatSinhThongTin.Text = ((Label)grid.Rows[e.NewEditIndex].FindControl("lblngay")).Text;
            txtNoiDung.Text = ((Label)grid.Rows[e.NewEditIndex].FindControl("lblnoidung")).Text;
        }
        protected void BindGrid()
        {
            string str = Request.QueryString["id"].Replace(" ", "+");
            string madetai = _ps.Decrypt(str, "gold", 128);
            var list = from p in _db.tblGhiChus.Where(r => r.MaDeTai.Trim() == madetai)
                       select new
                       {
                           ID=p.ID,
                           NgayGhiChu=string.Format("{0:dd/MM/yyyy}",p.NgayGhiChu),
                           NoiDung=p.NoiDung.Trim(),
                           Lan=p.Lan
                       };
            grid.DataSource = list.ToList();
            grid.DataBind();
        }
    }
}